package com.cndw.kungfu.model.skill;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Skill;
import com.cndw.kungfu.domain.SkillMode;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.PlayerBoxs;

/**
 * 玩家的技能
 * 
 * @author fantadust
 * @date 2011-5-5 下午09:20:30
 */
public class PlayerSkill {
	/**
	 * 用户id
	 */
	private long playerId;
	/**
	 * 技能数组
	 */
	private Map<Integer, Skill> skillMap = new HashMap<Integer, Skill>();
	/**
	 * 我的技能格子
	 */
	private Map<Integer, SkillGird> shortcutMap = new HashMap<Integer, SkillGird>();
	/**
	 * 技能公共cd时间
	 */
	private int commCd = 0;
	public static final int skillGirdMax = 7;
	/**
	 * 上次使用时间
	 */
	private Map<Integer, Integer> lastTimesMap = new HashMap<Integer, Integer>();
	private int lastGirdId;

	public PlayerSkill(long playerId) {
		this.playerId = playerId;
	}

	public Map<Integer, Skill> getSkillMap() {
		return skillMap;
	}

	public int getLevel(int libId) {
		if (!skillMap.containsKey(libId)) {
			return 0;
		}
		return skillMap.get(libId).getLevel();
	}

	public void setSkillMap(Map<Integer, Skill> skillMap) {
		this.skillMap = skillMap;
	}

	public int getCommCd() {
		return commCd;
	}

	public void setCommCd(int commCd) {
		this.commCd = commCd;
	}

	public void clacLastTimes() {
		lastTimesMap.clear();
	}

	public synchronized Collection<SkillGird> getShortcutGirds() {
		return shortcutMap.values();
	}
	
	public Map<Integer, SkillGird> getShortcutMap() {
		return shortcutMap;
	}

	public Collection<Skill> getsSkills() {
		return skillMap.values();
	}

	public void initShortcut() {
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player == null) {
			return;
		}
		String shortcut = player.getShortcut();
		if (shortcut == null) {
			return;
		}
		int[] ids = StringProcesser.toIntArray(shortcut);
		if (ids == null || ids.length <= 0) {
			return;
		}
		for (int i = 0; i < ids.length; i++) {
			int j = ids[i];
			int k = i + 1;
			if (j > 0) {
				SkillGird skillGird = new SkillGird();
				skillGird.setPosition(k);
				skillGird.setId(j);
				if (j < Constants.skillDifferenceGoods) {
					skillGird.setAreGoods(true);
				}
				shortcutMap.put(skillGird.getPosition(), skillGird);
			}

		}
	}

	public synchronized boolean shortcutPut(SkillGird skillGird) {
		if (skillGird == null) {
			return false;
		}
		shortcutMap.put(skillGird.getPosition(), skillGird);

		syncDB();
		return true;
	}
	
	private synchronized void syncDB(){
		String[] shortcutArr = new String[skillGirdMax];
		for (SkillGird e : shortcutMap.values()) {
			if (e.getPosition() <= skillGirdMax) {
				shortcutArr[e.getPosition() - 1] = String.valueOf(e.getId());
			}
		}
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		player.setShortcut(StringProcesser.toString(shortcutArr));
	}

	public synchronized boolean shortcutOrder(SkillGird skillGird) {
		if (skillGird == null) {
			return false;
		}
		for (int i = PlayerSkill.skillGirdMax - 1; i >= 1; i--) {
			if (shortcutGet(i) != null && shortcutGet(i + 1) == null) {
				SkillGird skillGird2 = shortcutGet(i);
				skillGird2.setPosition(i + 1);
				shortcutMap.put(skillGird2.getPosition(), skillGird2);
				shortcutMap.remove(i);
			}
		}
		if (skillMap.get(1) == null) {
			skillGird.setPosition(1);
			shortcutMap.put(skillGird.getPosition(), skillGird);
		}
		syncDB();
		return true;
	}

	public synchronized SkillGird shortcutGet(int position) {
		return shortcutMap.get(position);
	}

	public synchronized SkillGird shortcutRemove(int position) {
		return shortcutMap.remove(position);
	}

	public void setLastGirdId(int lastGirdId) {
		this.lastGirdId = lastGirdId;
	}

	public SkillGird getNextSkillGird(int nowstep, int sourceMp) {

		SkillGird skillGird = null;
		if (shortcutMap.size() == 0) {
			return null;
		}
		PlayerBoxs playerBoxs = Context.getGoodsServer().getPlayerBoxs(playerId);
		int times = 0;
		for (int i = lastGirdId + 1; i <= skillGirdMax; i++) {
			times++;
			if (times > 9) {
				break;
			}
			SkillGird tmpGird = shortcutMap.get(i);
			if (i == skillGirdMax) {// 重新定位到初始
				i = 0;
			}
			if (tmpGird == null) {
				continue;
			} else {
				int tmpGridId = tmpGird.getId();

				int coolingTime = 0;
				if (tmpGird.isAreGoods()) {
					if (playerBoxs.getStackNum(tmpGridId) <= 0) {// 物品已没有
						continue;
					}
					GoodsMode goodsMode = Context.getGoodsServer().getModeById(tmpGridId);
					if (goodsMode == null) {
						continue;
					}
					coolingTime = goodsMode.getCoolingTime();

				} else {
					SkillMode skillMode = Context.getSkillServer().getSkillMode(tmpGridId);
					if (skillMode == null || sourceMp < skillMode.getConstMp()) {
						continue;
					}
					coolingTime = skillMode.getCoolingTime();
				}
				if (coolingTime > 0) {
					int lastTime = 0;
					if (lastTimesMap.containsKey(tmpGird)) {
						lastTime = lastTimesMap.get(tmpGridId);
					}

					if (lastTime == 0 || lastTime + coolingTime <= nowstep) {
						lastTimesMap.put(tmpGridId, nowstep);
						skillGird = tmpGird;
						if (tmpGird.isAreGoods()) {
							playerBoxs.costGoods(tmpGridId, 1);
						}
					} else {
						continue;
					}
				} else {
					skillGird = tmpGird;
				}

			}
			if (skillGird != null) {
				lastGirdId = i;// 标志上次使用的技能
				break;
			}
		}

		return skillGird;

	}

	public SkillGird getSkillGird(int nowstep, int sourceMp) {
		if (shortcutMap.size() == 0) {
			return null;
		}

		do {
			lastGirdId++;
			lastGirdId = lastGirdId > skillGirdMax ? 1 : lastGirdId;
		} while (!shortcutMap.containsKey(lastGirdId));

		SkillGird skillGird = shortcutMap.get(lastGirdId);
		if (skillGird == null) {
			return null;
		}
		int coolingTime = 0;
		int tmpGridId = skillGird.getId();
		PlayerBoxs playerBoxs = null;
		if (skillGird.isAreGoods()) {
			playerBoxs = Context.getGoodsServer().getPlayerBoxs(playerId);
			if (playerBoxs.getStackNum(tmpGridId) <= 0) {// 物品已没有
				return null;
			}
			GoodsMode goodsMode = Context.getGoodsServer().getModeById(tmpGridId);
			if (goodsMode == null) {
				return null;
			}
			coolingTime = goodsMode.getCoolingTime();
		} else {
			SkillMode skillMode = Context.getSkillServer().getSkillMode(tmpGridId);
			if (skillMode == null || sourceMp < skillMode.getConstMp()) {
				return null;
			}
			coolingTime = skillMode.getCoolingTime();
		}
		if (coolingTime > 0) {
			int lastTime = 0;
			if (lastTimesMap.containsKey(tmpGridId)) {
				lastTime = lastTimesMap.get(tmpGridId);
			}
			if (lastTime == 0 || lastTime + coolingTime < nowstep) {
				lastTimesMap.put(tmpGridId, nowstep);
			} else {
				return null;
			}
		}
		if (skillGird.isAreGoods()) {
			playerBoxs.costGoodsSilent(tmpGridId, 1, true);
		}
		return skillGird;

	}

}
